mini.parse();
var grid=mini.get("datagrid1");
var searchCols=mini.get("searchCols");
var grid2=mini.get("datagrid2");
var saveBtn=mini.get("saveBtn");
var data6;
//自动执行
$(function () {
    init();             //初始化表格
    searchColsinit();   //加载下拉框
})
//初始化加载数据方法
function init() {
    $.ajax({
        url:ajaxUrl + '/user/list.do',
        type: 'POST',
        success: function (data) {
            console.log(data);
            var data1=data.data;
            grid.setData(data1);
            grid.mask({                             //自动加载表格grid时有圆圈遮罩
                html : "<div></div>",
                cls : "mini-panel",
                style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
            });
        },
        error: function (data) {
            console.log("请求失败")
        }
    });
}
//下拉框加载数据
function searchColsinit() {
    $.ajax({
        url:ajaxUrl + '/data/finduserId.do',
        type: 'POST',
        success: function (data) {
            console.log(data);
            data6=mini.clone(data);
            var data1=data;
            searchCols.setData(data1);


        },
        error: function (data) {
            console.log("请求失败")
        }
    });
}
//当下拉框改变时请求接口
function search() {
    grid.unmask();             //取消表格grid的遮罩
    var data=mini.get("searchCols").getValue();  //获得下拉框选择的对应的值
    console.log(data);
    data3=mini.clone(data);    //克隆一下在下面要做参数
    data4=mini.clone(data);    //克隆它用在保存那
    data11=mini.clone(data);
    $.ajax({
        url:ajaxUrl + '/formltems/findsAll.do',
        type: 'POST',
        data:{
            formListId:data
        },
        success: function (data) {
            console.log(data);
            cTableName=data[2].tableName;
            console.log(cTableName);
            grid2.set({
                url: "",
                style: "width:100%;height:100%;",
                columns: data,
                multiSelect: true,
                allowResize: true,
                allowCellEdit: false,
                allowCellSelect: true,
                cellEditAction: "cellclick",
                allowRowSelect: true,
                showPager: false,
                editNextOnEnterKey: true,
                editNextRowCell: true
            });
            $.ajax({
                url:ajaxUrl + '/sqlAction/findsAll.do',
                type: 'POST',
                data:{
                    formListId:data3
                },
                success: function (data) {
                    console.log(data);
                    var data4=JSON.parse(data);
                    console.log(data4);
                    grid2.setData(data4);
                    $.ajax({
                        url:ajaxUrl + '/data/finds.do',
                        type:"post",
                        data:{
                            formListId:data11,
                            userName:data13
                        },
                        success:function (data) {
                            console.log(data);
                            var rowArr2=[];
                            data.forEach(function (item) {
                                var row=grid2.findRow(function (row) {
                                    if(item.Id==row[cTableName+'_id'])
                                        return true
                                })
                                rowArr2.push(row)
                            })
                            console.log(rowArr2);
                            grid2.selects(rowArr2,true);
                        },
                        error:function (data) {
                        }
                    })
                },
                error:function (data) {
                    alert("444")
                }
            });
        },
        error: function (data) {
            console.log("请求失败")
        }
    });
}
//保存方法
function save() {
    var dataarr=[];
    var row1=grid.getSelecteds();
    console.log(row1);
    console.log(cTableName);
    var row2=grid2.getSelecteds();
    console.log(row2);
    var data10=[];
    for (var i = 0, l = row2.length; i < l; i++) {
        var r = row2[i];
        data10.push(r[cTableName+'_id']);
    }
    console.log(data10);
    var dataJson={
        cTableName:data4,
        userName:row1[0].userName,
        userCode:row1[0].userCode,
        arrlist:data10
    }
    console.log(dataJson);
    dataJson=JSON.stringify(dataJson);
    console.log(dataJson);
    $.ajax({
        url:ajaxUrl + '/data/inserts.do',
        type:"post",
        data:{
            datas:dataJson
        },
        success:function (data) {
            console.log(data);
            mini.alert(data.success);
        },
        error:function (data) {
            console.log(data);
            alert("失败");
        }
    })
}
//当选中表格grid时触发事件
function SelectionChanged() {
    saveBtn.enable();

    grid2.clearSelect();        //grid2取消所有当前打勾状态
    var row4=grid.getSelecteds();
    data13=mini.clone(row4[0].userName);
    row5=mini.clone(row4);
    console.log(row4);
    mini.get("searchCols").enable();
    searchCols.unmask();
    $.ajax({
        url:ajaxUrl + '/data/finds.do',
        type:"post",
        data:{
            formListId:data11,
            userName:data13
        },
        success:function (data) {
            console.log(data);
            var rowArr2=[];
            data.forEach(function (item) {
                var row=grid2.findRow(function (row) {
                    if(item.Id==row[cTableName+'_id'])
                        return true
                })
                rowArr2.push(row)
            })
            console.log(rowArr2);
            grid2.selects(rowArr2,true);
        },
        error:function (data) {
        }
    })
}
// //表格自动选择事件，要求grid2至少一个或者多个以上打勾，才能按保存
// function tosave() {
//    var rows=grid2.getSelecteds();
//     console.log(rows.length);
//    if(rows.length>=1&&row5.length>=1){
//        saveBtn.enable();
//    }else{
//        mini.get("saveBtn").disable();
//    }
// }
function tosave() {

}